package com.huxiu;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.huxiu.utils.ErrorResponseException;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by tian on 14-9-20:下午10:16.
 * 基类
 */
public class BaseActivity extends FragmentActivity {

    public static RequestQueue mQueue;
    // 主体显示
    protected ViewGroup mainBody;

    public LinearLayout reLoadView;

    // 是否使用模板 true 使用模板，false 不使用模板
    protected boolean isTemplate = false;

    //往ProfileFragmentQcl中传递mQueue变量
    public static RequestQueue getmQueue() {
        return mQueue;
    }

    public void setmQueue(RequestQueue mQueue) {
        this.mQueue = mQueue;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mQueue = Volley.newRequestQueue(this);
        ViewUtils.inject(this);
        mainBody = (ViewGroup) findViewById(R.id.view_mainBody);
        reLoadView = (LinearLayout) findViewById(R.id.reLoadView);
        reLoadView.setVisibility(View.GONE);

    }

    @Override
    protected void onDestroy() {
        mQueue.cancelAll(this);
        super.onDestroy();
    }


    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID == R.layout.activity_base) {
            super.setContentView(layoutResID);
        } else {
            mainBody.removeAllViews();
            mainBody.addView(this.getLayoutInflater()
                    .inflate(layoutResID, null));
        }
    }

    @Override
    public void setContentView(View view) {
        mainBody.removeAllViews();
        mainBody.addView(view);
    }

    protected ErrorListener mErrorListener = new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Throwable t = error.getCause();
            if (t instanceof ErrorResponseException) {
                //                Log.e("params", "message" + error.getMessage().toString());
                Utils.showToast(error.getMessage());
            } else {
                Utils.showToast(R.string.generic_check_online);
            }

        }
    };

    public void updateWebViewSettings(WebView web) {
        WebSettings webSettings = web.getSettings();
        //        webSettings.setUseWideViewPort(false);
        //        webSettings.setLoadWithOverviewMode(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        Utils.setDefaultWebSettings(web);
    }

    public void webviewSetContent(WebView web, String content) {
        web.loadData(content, "text/html; charset=UTF-8", null);
    }


    @OnClick(R.id.reLoadView)
    public void mOnClick(View v) {
        switch (v.getId()) {
            case R.id.reLoadView:

                break;
        }
    }
}
